﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace MagicCard
{
    public partial class frmBaseCardAdd : Form
    {
        private string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MagicCard.accdb";
        private OleDbConnection conn = null;
        private AccessDao accessDao = null;
        private string categoryID = string.Empty;

        public frmBaseCardAdd(string srcID)
        {
            InitializeComponent();
            categoryID = srcID;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int cardValue;
            if (!int.TryParse(txtCardValue.Text.Trim(), out cardValue))
            {
                MessageBox.Show("卡片面值必须是整数", "错误");
                return;
            }
            if (txtCardName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("卡片名称不能为空，请重新输入！", "错误");
                return;
            }
            if (accessDao.IsDoubleName(txtCardName.Text.Trim()))
            {
                MessageBox.Show("卡片名称已经存在，请重新输入！", "错误");
                return;
            }

            StringBuilder commStr = new StringBuilder();
            commStr.Append("INSERT INTO MC_Card  (CardCategory,CardName,CardLv,Requirements,CardValue,CardTime,Base1,Base2,Base3)  values");
            commStr.Append("(" + categoryID);
            commStr.Append(",'" + txtCardName.Text.Trim() + "'");
            commStr.Append(",0");
            commStr.Append(",1");
            commStr.Append("," + cardValue);
            commStr.Append(",0");
            commStr.Append(",-1,-1,-1)");

            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                OleDbCommand comm = new OleDbCommand(commStr.ToString(), conn);
                try
                {
                    int ret = comm.ExecuteNonQuery();
                    if (ret == 1)
                    {
                        MessageBox.Show("卡片增加成功！", "成功");
                        if (chkContinue.Checked)
                        {
                            txtCardName.Text = "";
                            txtCardValue.Text = "";
                        }
                        else
                        {
                            this.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("卡片增加失败！", "失败");
                    }
                }
                catch (Exception ex)
                {
                }
            }

        }

        private void frmBaseCardAdd_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection(connStr);
            accessDao = new AccessDao(conn);
        }
    }
}
